package com.task.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtils implements ApplicationContextAware {
    private static final Logger log = LoggerFactory.getLogger(SpringContextUtils.class);
    public static ApplicationContext applicationContext;

    public SpringContextUtils() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    public static Collection<Object> getBeanByAnnotation(Class<? extends Annotation> c) {
        Map<String, Object> map = applicationContext.getBeansWithAnnotation(c);
        return new ArrayList(map.values());
    }

    public static <T> Collection<T> getBeanByType(Class<T> c) {
        Map<String, T> map = applicationContext.getBeansOfType(c);
        return new ArrayList(map.values());
    }

    public static Object getBean(String name) {
        return applicationContext == null ? null : applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> requiredType) {
        assertContextInjected();
        return applicationContext == null ? null : applicationContext.getBean(requiredType);
    }

    public static <T> T getBean(String name, Class<T> requiredType) {
        assertContextInjected();
        return applicationContext == null ? null : applicationContext.getBean(name, requiredType);
    }

    public static boolean containsBean(String name) {
        return applicationContext == null ? null : applicationContext.containsBean(name);
    }

    public static boolean isSingleton(String name) {
        return applicationContext == null ? null : applicationContext.isSingleton(name);
    }

    public static Class<? extends Object> getType(String name) {
        return applicationContext == null ? null : applicationContext.getType(name);
    }

    public static <T> T getBean(Class<T> type, String name) {
        assertContextInjected();
        return applicationContext == null ? null : applicationContext.getBean(name, type);
    }

    public static <T> Map<String, T> getBeansOfType(Class<T> type) {
        assertContextInjected();
        return applicationContext == null ? null : applicationContext.getBeansOfType(type);
    }

    public static <T> Collection<T> getAllBeansOfType(Class<T> type) {
        assertContextInjected();
        return applicationContext == null ? null : applicationContext.getBeansOfType(type).values();
    }

    public static Resource getResource(String path) {
        assertContextInjected();
        return applicationContext == null ? null : applicationContext.getResource(path);
    }

    public static InputStream getResourceStream(String path) {
        assertContextInjected();

        try {
            return applicationContext == null ? null : applicationContext.getResource(path).getInputStream();
        } catch (IOException var2) {
            throw new RuntimeException("load resource error.", var2);
        }
    }

    public static void assertContextInjected() {
        if (applicationContext == null) {
            log.warn("applicationContext未注入");
        }

    }
}
